import React from 'react';
import {injectIntl} from 'react-intl';
import {Form, Row, Col, Input, Button, Divider, message} from 'antd';
import Grid from '../../components/table/grid';
import {Link} from 'react-router-dom';
import axios from 'axios';

const FormItem = Form.Item;
const createForm = Form.create;

class monitorPlan extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            refresh: 0,
            params: {}
        };
        const common = this.props.intl.messages.common;
        this.columns = [{
            title: common.taskName,
            dataIndex: 'name',
            key: 'name',
        }, {
            title: common.launchTime,
            dataIndex: 'startTime',
            key: 'startTime',
        }, {
            title: common.createTime,
            dataIndex: 'createTime',
            key: 'createTime',
        }, {
            title: common.operation,
            dataIndex: 'operation',
            key: 'operation',
            render: (text, record) => (
                <span>
                    <Link to={{pathname: "/monitor/addTask", state: {status:"modify",info:record}}}>
                    {common.modify}
                    </Link>
                    <Divider type="vertical"/>
                    <a onClick={() => this.deleteTask(record)}>{common.delete}</a>
                </span>

            ),
        }
        ]
    }


    deleteTask = (record) => {
        let params = {
            planId: record.id,
            status: 9
        }
        const common = this.props.intl.messages.common;
        axios.get('/api/plan/info/mod', {params:params,loading:true}).then(res => {
            if (res && res.data &&res.data.code=="200") {
                message.success(common.delSuc);
                this.setState({
                    refresh: this.state.refresh + 1
                })
            }
        })
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((errors, values) => {
            if (!!errors) {
                console.log('Errors in form!!!');
                return;
            }
            console.log(values)
            this.setState({
                params: {...values},
                refresh: this.state.refresh + 1
            });
        });
    };

    render() {
        const common = this.props.intl.messages.common;
        const {getFieldDecorator} = this.props.form;
        return (
            <div>
                <Row gutter={16}>
                    <Col xxl={3} xl={5}>
                        <FormItem
                        >
                            {getFieldDecorator('stubId', {})(
                                <Input placeholder={common.EquipmentNo}/>
                            )}
                        </FormItem>
                    </Col>
                    <Col xxl={3} xl={5}>
                        <FormItem
                        >
                            {getFieldDecorator('name', {})(
                                <Input placeholder={common.taskName}/>
                            )}
                        </FormItem>
                    </Col>
                    <Col xxl={18} xl={14}>
                        <FormItem>
                            <Button type="primary" className="ant-btn-84"
                                    onClick={this.handleSubmit}>{common.search}</Button>
                            <Link to={{pathname: "/monitor/addTask", state: {status:"add",info:null}}}>
                                <Button type="primary" className="ant-btn-84">{common.addTask}</Button>
                            </Link>
                        </FormItem>
                    </Col>
                </Row>
                <Grid columns={this.columns} url="/api/plan/qry" refresh={this.state.refresh}
                      params={this.state.params}></Grid>
            </div>
        )
    }
}

monitorPlan = createForm()(monitorPlan);
export default injectIntl(monitorPlan);